package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.common.article.ApArticle;
import com.heima.model.common.article.ApArticleConfig;
import com.heima.model.common.article.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.article.ArticleHomeDto;
import com.heima.model.common.wemedia.pojo.WmNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper articleMapper;
    @Autowired
    private ApArticleContentMapper contentMapper;
    @Autowired
    private ApArticleConfigMapper configMapper;

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;
    /**
     * 根据参数加载文章列表
     * @param dto
     * @return
     */
    @Override  //加载主页  loaddir 1刷新 0加载新的 2 加载更多
    public ResponseResult load(ArticleHomeDto dto) {
        //1.校验参数
        Integer size = dto.getSize();
        if(size == null || size == 0){
            size = 10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);

        List<ApArticle> articleList = articleMapper.load(dto);
        return ResponseResult.okResult(articleList);
    }

    @Override
    public ResponseResult add(WmNews wmNews, String authorName, String channelName) {
        ApArticle apArticle = new ApArticle();
        apArticle.setTitle(wmNews.getTitle());
        apArticle.setAuthorId(wmNews.getUserId().longValue());
        apArticle.setAuthorName(authorName); // 作者名
        apArticle.setChannelId(wmNews.getChannelId());
        apArticle.setChannelName(channelName); // 频道名
        //布局需要处理 news的布局为 0 无图文章 1 单图文章 3 多图文章
        //article中的布局         0 无图文章 1 单图文章 2 多图文章
        if (wmNews.getType()==3){
            apArticle.setLayout((short)2);
        }else {
            apArticle.setLayout(wmNews.getType()); //布局
        }
        apArticle.setImages(wmNews.getImages());
        apArticle.setLabels(wmNews.getLabels());
        apArticle.setCreatedTime(wmNews.getCreatedTime());
        apArticle.setPublishTime(wmNews.getPublishTime());
        //新增文章表
        articleMapper.insert(apArticle);
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(apArticle.getId());
        apArticleConfig.setIsComment(true);
        apArticleConfig.setIsForward(true);
        apArticleConfig.setIsDown(false);
        apArticleConfig.setIsDelete(false);
        //新增文章配置表
        configMapper.insert(apArticleConfig);
        ApArticleContent articleContent = new ApArticleContent();
        articleContent.setArticleId(apArticle.getId());
        articleContent.setContent(wmNews.getContent());
        //新增文章内容表
        contentMapper.insert(articleContent);

        return ResponseResult.okResult(apArticle.getId());
    }
}
